<?php

namespace App\Http\Requests\Center;

use App\Http\Requests\AbstractRequest;
use App\Models\User;

class sendVerifyLinkRequest extends AbstractRequest
{
    /**
     * 验证规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required|email'
        ];
    }

    /**
     * 自定义验证
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validate()
    {
        $email = $this->input('email');

        if ($email === $this->user()->email) {
            $this->validationException([
                'email' => ['新邮箱不能与旧邮箱相同']
            ]);
        } elseif (User::where('email', $email)->count() > 0) {
            $this->validationException([
                'email' => ['该邮箱已被其他用户绑定']
            ]);
        }
    }
}
